package LinkList02;

import javax.naming.directory.SearchControls;
import java.util.List;
import java.util.WeakHashMap;

public class MySingleList {
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }


    public ListNode head;


    public void createList(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;

        //成大事者务必拘束小节
        //学习找工作

        //自控能力强的人不会补录播

    }




    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }


    //是否包含关键字key
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
        }
        return false;
    }













    //打印单链表中的所有元素
    public void display(){
        ListNode cur = head;
        while(cur  != null){
            System.out.print(cur.val + " ");
            cur  = cur.next;
        }
        System.out.println();
    }

    //头插法
    public void addFirst(int data){
        ListNode cur = new ListNode(data);
        cur.next = head;
        head = cur;
    }

    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(cur == null){
            head  = node;
            return;
        }
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;

    }

    //在任意位置插入元素, 第一个数据节点为0号下标
    //定义cur走index - 1 步, 找到插入位置的前一个节点
    //进行插入
    public void addIndex(int index, int data){
        //先进行下标合法性的判断. 是否小于0 或者是否大于实际长度
        if(index < 0 || index > size()){
            System.out.println("index 不合法");
            return ;
        }
        //在第一个位置插入
        if(index == 0){
            addFirst(data);
            return;
        }
        //在最后一个位置插入
        if(index == size()){
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = head;
        while((index - 1) != 0){
            cur = cur.next;
            index--;
        }
        node.next = cur.next;
        cur.next = node;

    }


    //找到前一个节点
    public ListNode sesrchPrev(int data){
        ListNode cur = head;
        while(cur.next != null){
            if(cur.next.val == data){
                return cur;
            }
        }
        return null;
    }


    //删除一个节点
    public void remove(int data){
        if(head == null){
            return ;
        }

        if(head.val == data){
            head.next = head;
            return;
        }
        ListNode cur = sesrchPrev(data);
        if(cur == null){
            System.out.println("没有你要删除的节点");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;

    }

    //删除这个表中所有和这个值相同的节点

    public void removeAll(int data){
        ListNode cur = head.next;
        ListNode prev = head;
        ListNode node = new ListNode(data);

        while (cur != null) {
            if(cur.val == data){
                prev.next = cur.next;
                cur = cur.next;
            }
            else{
                prev = cur;
                cur = cur.next;
            }
        }

        if(head.val == data){
            head  =head.next;
        }
    }


    //清空链表
    public void clear(){
        this.head = null;
    }


}
